﻿using SilverPOP.Data.Process.DAL;
using SilverPOP.Data.Process.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SilverPOP.Data.Process.BLL
{
   public class DatabaseSchema
    {
        public static List<DatabaseSchemaEntity> GetDatabaseSchemaByProjectId(int projectId)
        {
            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@ProjectId", projectId));
            List<DataReaderObject> readerObject = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("spr_GetDatabaseFieldsByProjectId", spParamValue);

            if (readerObject != null && readerObject.Count > 0)
            {
                List<DatabaseSchemaEntity> _fieldList = new List<DatabaseSchemaEntity>();

                foreach (DataReaderObject dataReader in readerObject)
                {
                    DatabaseSchemaEntity _fieldInfo = new DatabaseSchemaEntity()
                    {

                        KeyIndex =(int)dataReader.ColumnValueList[0],
                        FieldName = dataReader.ColumnValueList[1] != DBNull.Value ? dataReader.ColumnValueList[1].ToString() : String.Empty,
                        FieldType = (int)dataReader.ColumnValueList[2],
                        IsInclude = dataReader.ColumnValueList[3] != DBNull.Value ? Convert.ToBoolean(dataReader.ColumnValueList[3].ToString()) : false,
                        IsKeyColumn = dataReader.ColumnValueList[4] != DBNull.Value ? Convert.ToBoolean(dataReader.ColumnValueList[4].ToString()) : false,
                        IsRequired = dataReader.ColumnValueList[5] != DBNull.Value ? Convert.ToBoolean(dataReader.ColumnValueList[5].ToString()) : false,
                        IsDefault = dataReader.ColumnValueList[6] != DBNull.Value ? Convert.ToBoolean(dataReader.ColumnValueList[6].ToString()) : false  ,
                       // IsWithContactList = dataReader.ColumnValueList[7] != DBNull.Value ? Convert.ToBoolean(dataReader.ColumnValueList[7].ToString()) : false         
                    };

                    _fieldList.Add(_fieldInfo);
                }

                return _fieldList;
            }
            else
            {
                return null;
            }
        }
    }
}
